* Estimation price impact with conditioning variables in single stage as panel for each day - 30 minute intervals;

libname this '\AuctionPriceImpact\Version2025';

data minor01; set this.taq_permno_cusip_linktable; keep symbol_15 date permno; 
data minor01; set minor01(rename=(symbol_15=sym_this)); 
proc sort data = minor01; by date permno;
run;

proc import datafile="TAQ.txt"
   out=minor02
   dbms=tab
   replace;
   getnames=no;
data minor02(rename=(VAR1=permno VAR2=ymd VAR3=ShrOut VAR4=LagADT VAR5=LagLnPrice VAR6=LagVolat VAR7=SZCAT VAR8=NASD VAR=EAD)); 
	set minor02;
data minor02; 
	set minor02;
	format date yymmddn.;
	date=input(put(ymd,8.),yymmdd8.);
	drop ymd;
proc sort data=minor02; by date permno;
run;

data link;
	merge minor01 (in=a) minor02 (in=b);
  	by date permno;
  	if a and b;
run;

%macro OneMonth(dname);
	data minor01a; 
		set this.&dname; 
		if timeperiod>=0 and timeperiod<390;
		timeperiod30 = ceil((timeperiod+1)/30);
		keep sym_this date timeperiod timeperiod30 Buys_v Sells_v Price;
	proc sort data=minor01a; 
		by sym_this date timeperiod30;
	run;

	proc means data=minor01a noprint; 
		by sym_this date timeperiod30;
		var Buys_v Sells_v;
		output out=minor01b(drop = _TYPE_ _FREQ_) sum(Buys_v)= Buys_v sum(Sells_v)= Sells_v;
	data minor01c; 
		set minor01a; 
		by sym_this date timeperiod30; if last.timeperiod30; 
		keep sym_this date timeperiod30 Price;
	data minor01d;
		merge minor01b(in=b) minor01c(in=c);
	  	by sym_this date timeperiod30;
		if b and c;
	run;

	data minor01e; /* create data for first price of the day */
		set minor01a; 
		by sym_this date timeperiod30; if first.timeperiod30; 
		keep sym_this date timeperiod30 Price;
	data minor01e; set minor01e(where=(timeperiod30=1));
	data minor01e; set minor01e; timeperiod30 = 0;
	run;

	data minor01f;
		set minor01d minor01e;
	proc sort data=minor01f; by sym_this date timeperiod30;
	run;

	data minor01f;
		set minor01f;
		Lsym_this = lag(sym_this);
		Ldate = lag(date);
		Ltimeperiod30 = lag(timeperiod30);
		Lprice = lag(Price);
	run;

	data minor02;
		set minor01f;
		if sym_this = Lsym_this and date = Ldate and timeperiod30 = Ltimeperiod30 + 1 then
			do;
				Ret = Price/LPrice - 1;
				q = Buys_v - Sells_v;
			end;
		else
			do;
				Ret = .;
				q = .;
			end;

		keep sym_this date timeperiod30 Ret q;
	data minor02; set minor02(where=(timeperiod30>0));
	run;

	proc sql;
		create table minor03 as 
			select *
			from minor02, link
			where minor02.sym_this=link.sym_this and minor02.date=link.date;
	quit;

	data minor04a; 
		set minor03; 
		X = (q/Shrout)/LagADT; 

		if X<-0.1 then X=.; if X>0.1 then X=.;
		if Ret<-0.1 then Ret=.; if Ret>0.1 then Ret=.; 

		drop sym_this Shrout LagADT q;
	proc sort data=minor04a; by date permno timeperiod30;
	run;

	data minor04;
		set minor04a;

		Ret = 100*Ret;
		SqrtOIB = sign(X)*sqrt(abs(X));
		X1 = 100*X;
		X2 = 100*SqrtOIB;

		ym = year(date)*100 + month(date);

		drop LagLnPrice LagVolat SZCAT NASD X SqrtOIB EAD; 
		proc sort data=minor04; by permno ym date timeperiod30;
	run;

	proc summary data = minor04;
	  where X1=0;
	  by permno ym;
	  output out = stat0(rename=(_freq_=Nzero) drop=_TYPE_);
	run;

	/* Linear */
	proc reg data=minor04 outest=stat1 ADJRSQ EDF noprint;
		by permno ym;
		model Ret = X1;
	data stat1; 
		set stat1(rename=(X1=Lambda1 _ADJRSQ_=RSQ1)); 
		if _EDF_<50 then delete; 
		Nobs1 = _P_ + _EDF_;
		keep permno ym Lambda1 RSQ1 Nobs1;
	run;

	/* Square this */
	proc reg data=minor04 outest=stat2 ADJRSQ EDF noprint;
		by permno ym;
		model Ret = X2;
	data stat2; 
		set stat2(rename=(X2=Lambda2 _ADJRSQ_=RSQ2)); 
		if _EDF_<50 then delete; 
		Nobs2 = _P_ + _EDF_;
		keep permno ym Lambda2 RSQ2 Nobs2;
	run;

	data stat3;
		merge stat0(in=a) stat1(in=b) stat2(in=c);
	  	by permno ym;
		if b or c;
	proc append base=final data=stat3;
	run;

%mend;

proc delete data = final; run;

%OneMonth(taq_month201201); %OneMonth(taq_month201202); %OneMonth(taq_month201203); %OneMonth(taq_month201204);
%OneMonth(taq_month201205); %OneMonth(taq_month201206); %OneMonth(taq_month201207); %OneMonth(taq_month201208);
%OneMonth(taq_month201209); %OneMonth(taq_month201210); %OneMonth(taq_month201211); %OneMonth(taq_month201212);
%OneMonth(taq_month201301); %OneMonth(taq_month201302); %OneMonth(taq_month201303); %OneMonth(taq_month201304);
%OneMonth(taq_month201305); %OneMonth(taq_month201306); %OneMonth(taq_month201307); %OneMonth(taq_month201308);
%OneMonth(taq_month201309); %OneMonth(taq_month201310); %OneMonth(taq_month201311); %OneMonth(taq_month201312);
%OneMonth(taq_month201401); %OneMonth(taq_month201402); %OneMonth(taq_month201403); %OneMonth(taq_month201404);
%OneMonth(taq_month201405); %OneMonth(taq_month201406); %OneMonth(taq_month201407); %OneMonth(taq_month201408);
%OneMonth(taq_month201409); %OneMonth(taq_month201410); %OneMonth(taq_month201411); %OneMonth(taq_month201412);
%OneMonth(taq_month201501); %OneMonth(taq_month201502); %OneMonth(taq_month201503); %OneMonth(taq_month201504);
%OneMonth(taq_month201505); %OneMonth(taq_month201506); %OneMonth(taq_month201507); %OneMonth(taq_month201508);
%OneMonth(taq_month201509); %OneMonth(taq_month201510); %OneMonth(taq_month201511); %OneMonth(taq_month201512);
%OneMonth(taq_month201601); %OneMonth(taq_month201602); %OneMonth(taq_month201603); %OneMonth(taq_month201604);
%OneMonth(taq_month201605); %OneMonth(taq_month201606); %OneMonth(taq_month201607); %OneMonth(taq_month201608);
%OneMonth(taq_month201609); %OneMonth(taq_month201610); %OneMonth(taq_month201611); %OneMonth(taq_month201612);
%OneMonth(taq_month201701); %OneMonth(taq_month201702); %OneMonth(taq_month201703); %OneMonth(taq_month201704);
%OneMonth(taq_month201705); %OneMonth(taq_month201706); %OneMonth(taq_month201707); %OneMonth(taq_month201708);
%OneMonth(taq_month201709); %OneMonth(taq_month201710); %OneMonth(taq_month201711); %OneMonth(taq_month201712);
%OneMonth(taq_month201801); %OneMonth(taq_month201802); %OneMonth(taq_month201803); %OneMonth(taq_month201804);
%OneMonth(taq_month201805); %OneMonth(taq_month201806); %OneMonth(taq_month201807); %OneMonth(taq_month201808);
%OneMonth(taq_month201809); %OneMonth(taq_month201810); %OneMonth(taq_month201811); %OneMonth(taq_month201812);
%OneMonth(taq_month201901); %OneMonth(taq_month201902); %OneMonth(taq_month201903); %OneMonth(taq_month201904);
%OneMonth(taq_month201905); %OneMonth(taq_month201906); %OneMonth(taq_month201907); %OneMonth(taq_month201908);
%OneMonth(taq_month201909); %OneMonth(taq_month201910); %OneMonth(taq_month201911); %OneMonth(taq_month201912);
%OneMonth(taq_month202001); %OneMonth(taq_month202002); %OneMonth(taq_month202003); %OneMonth(taq_month202004);
%OneMonth(taq_month202005); %OneMonth(taq_month202006); %OneMonth(taq_month202007); %OneMonth(taq_month202008);
%OneMonth(taq_month202009); %OneMonth(taq_month202010); %OneMonth(taq_month202011); %OneMonth(taq_month202012);
%OneMonth(taq_month202101); %OneMonth(taq_month202102); %OneMonth(taq_month202103); %OneMonth(taq_month202104);
%OneMonth(taq_month202105); %OneMonth(taq_month202106); %OneMonth(taq_month202107); %OneMonth(taq_month202108);
%OneMonth(taq_month202109); %OneMonth(taq_month202110); %OneMonth(taq_month202111); %OneMonth(taq_month202112);

proc sort data=final; by permno ym;
data this.PI_Continuous;
	set final;
run;
